package com.itic.ht.mem.member.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.IticConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.common.controller.BaseController;
import com.itic.appbase.framework.exception.definition.ParameterErrorException;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.ht.mem.member.persistence.model.WebMember;
import com.itic.ht.mem.member.persistence.model.WebMemberExample;
import com.itic.ht.mem.member.service.WebMemberService;
import com.itic.ht.mem.memberdetail.persistence.model.WebMemberDetail;
import com.itic.ht.mem.memberdetail.service.WebMemberDetailService;
import com.itic.ht.mem.mtype.persistence.model.WebmType;
import com.itic.ht.mem.mtype.persistence.model.WebmTypeExample;
import com.itic.ht.mem.mtype.persistence.model.WebmTypeExample.Criteria;
import com.itic.ht.mem.mtype.service.WebmTypeService;
import com.itic.ht.mem.orderdetail.service.MemberOrderDetailService;


@Controller
@RequestMapping(value="/mem/member")
public class MemberController
     extends BaseController {
    @Autowired
    private WebmTypeService typeService;
    @Autowired
    private WebMemberService webMemberService;
    @Autowired
    private WebMemberDetailService webMemberDetailService;

    
    //////////////////////////////////////////////////////常量区////////////////////////////////////////////////////////////////
    private static final String MODEL_NAME_CHINA = "会员";//中文名
    private static final String MODEL_NAME＿ENGLISH = "member";//英文名
    private static final String PAGE_COMMON_PATH = "/mem/member/member";//通用路径
    private static final String COMMON_URL = "/mem/member/";//通用路径

    /*
     * 主页面
     */
    @RequestMapping(value="/init")
    public String init(Model model){
    	model.addAttribute("name_china", MODEL_NAME_CHINA);
    	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
    	model.addAttribute("common_url", COMMON_URL);
    	return PAGE_COMMON_PATH+"Main";
    }
    
    /*
     * 添加页面
     */
    @RequestMapping(value="/initAdd")
    public String initAdd(Model model){
    	model.addAttribute("name_china", MODEL_NAME_CHINA);
    	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
    	model.addAttribute("common_url", COMMON_URL);
    	return PAGE_COMMON_PATH+"Add";
    }
    
    /*
     * 编辑页面
     */
    @RequestMapping(value="/initEdit")
    public String initEdit(Integer id , Model model){
    	if( null != id ) {
    		WebMember temp = webMemberService.get(id);
    		model.addAttribute("editData",iticJsonMapper.toJson(temp));
    		model.addAttribute("name_china", MODEL_NAME_CHINA);
        	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
        	model.addAttribute("common_url", COMMON_URL);
    	}else {
            throw new ParameterErrorException();
        }
    	return PAGE_COMMON_PATH+"Edit";
    }
    
    /*
     * 查看页面
     */
    @RequestMapping(value="/initView")
    public String initView(Integer id , Model model){
    	if( null != id ) {
    		
    		WebMember temp  = webMemberService.get(id);
    		
    		model.addAttribute("viewEdit",iticJsonMapper.toJson(temp));
    		model.addAttribute("name_china", MODEL_NAME_CHINA);
        	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
        	model.addAttribute("common_url", COMMON_URL);
    	}else {
            throw new ParameterErrorException();
        }
    	
    	return PAGE_COMMON_PATH+"View";
    }
    
    /**
     * 
     * @param goldType
     * @param request
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Map<String, Object> list(WebMember temp, HttpServletRequest request){
        // 声明返回对象
        Map<String, Object> resultMap = new HashMap<String, Object>();
        
        // 参数处理
        WebMemberExample example = new WebMemberExample();
        // 按创建时间倒序
        example.setOrderByClause("ctime desc");
     // 查询条件
        if ( null != temp ) {
           if(StringHelper.isNotBlank(temp.getAccount())){
           	 example.createCriteria().andAccountLike("%"+temp.getAccount()+"%");
           }
           if(StringHelper.isNotBlank(temp.getCategory())){
           	example.createCriteria().andCategoryEqualTo(temp.getCategory());
           }
        }
        // 封装分页参数
        example = this.handlePageQuery(request, example);
        
        
        // 获取信息总条数
        int total = webMemberService.count(example);
        
        // 获取数据
        List<WebMember> list = webMemberService.list(example);
        if( list.size() > 0 ){
        	for( WebMember mem : list){
        		if( StringHelper.isNotBlank(mem.getCategory()) ){
        		    mem.setCategory(typeService.get(mem.getCategory()).getName());
        		    WebMemberDetail detail = webMemberDetailService.getMemberDetailByAccount(mem.getAccount());
        		    if( null != detail ){
        		    	 String pid = detail.getPid();
             		     mem.setPassword(pid);
        		    }
        		}
        	}
        	
        }
        //过滤黑名单字段   
        //resultList = ValueHelper.tranList2MapList(list, ignoreProperties);
        
        
        // 数据返回处理
        resultMap.put(IticConst.TOTAL_RECORDS, total);
        resultMap.put(IticConst.DISPLAY_RECORDS, total);
        resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
        resultMap.put(IticConst.DATA, list);
        return resultMap;

    }
    
    /**
     * 添加模板管理数据
     * @param android
     * @return
     */
    @RequestMapping(value="/add")
    @ResponseBody
    public Map<String,String> add(WebMember temp){
    	

    	 Map<String, String> resultMap = new HashMap<String, String>();
         String result = ResponseCode.SERVER_ERROR.getCode();
         temp.setExchangePoints(0);
         temp.setSumPoints(0);
         if( StringHelper.isNotBlank(temp.getCategory()) && null != temp.getExpireTime() ){
        	 WebMemberDetail webMemberDetail = webMemberDetailService.getMemberDetailByAccount(temp.getAccount());
        	 if( null != webMemberDetail ){
        		 webMemberDetail.setCategory( temp.getCategory() );
        		 webMemberDetail.setExpireTime(temp.getExpireTime());
        		 webMemberDetailService.edit(webMemberDetail);
        	 }else{
        		 webMemberDetailService.deleteOther(temp);
            	 WebMemberDetail memberDetail = new WebMemberDetail();
            	 memberDetail.setAccount(temp.getAccount());
            	 memberDetail.setCategory(temp.getCategory());
            	 memberDetail.setCtime(new Date());
            	 memberDetail.setMtime(new Date());
            	 memberDetail.setDelFlag(Integer.parseInt(DBConst.TRUE));
            	 memberDetail.setExpireTime( temp.getExpireTime() );
            	 memberDetail.setInvitationCode(temp.getAccount());
            	 webMemberDetailService.add(memberDetail);
        	 }
         }
         result = webMemberService.add(temp);
         
         resultMap.put(IticConst.RESULT, result);
         return resultMap;
    }
    /**
     * 编辑保存
     * @param templet
     * @return
     */
    @RequestMapping(value="/edit")
    @ResponseBody
    public Map<String,String> edit( WebMember temp){
    
    	
    	 Map<String, String> resultMap = new HashMap<String, String>();
         String result = ResponseCode.SERVER_ERROR.getCode();
         if( StringHelper.isNotBlank(temp.getCategory()) && null != temp.getExpireTime() ){
        	 WebMemberDetail webMemberDetail = webMemberDetailService.getMemberDetailByAccount(temp.getAccount());
        	 if( null != webMemberDetail ){
        		 webMemberDetail.setCategory( temp.getCategory() );
        		 webMemberDetail.setExpireTime(temp.getExpireTime());
        		 webMemberDetailService.edit(webMemberDetail);
        	 }else{
        		 webMemberDetailService.deleteOther(temp);
            	 WebMemberDetail memberDetail = new WebMemberDetail();
            	 memberDetail.setAccount(temp.getAccount());
            	 memberDetail.setCategory(temp.getCategory());
            	 memberDetail.setCtime(new Date());
            	 memberDetail.setMtime(new Date());
            	 memberDetail.setDelFlag(Integer.parseInt(DBConst.TRUE));
            	 memberDetail.setExpireTime( temp.getExpireTime() );
            	 memberDetail.setInvitationCode(temp.getAccount());
            	 webMemberDetailService.add(memberDetail);
        	 }
         }
         result = webMemberService.edit(temp);
         
         resultMap.put(IticConst.RESULT, result);
         return resultMap;
    }
    
    /**
     * 删除
     * 
     */
    @RequestMapping(value = "/del")
    @ResponseBody
    public Map<String, String> del(Integer id) {
        Map<String, String> resultMap = new HashMap<String, String>();
        String result = ResponseCode.SERVER_ERROR.getCode();

        if( null != id ){
        	/*Htuser temp = new Htuser();
        	temp.setId(id);
            result = HtuserService.delete(temp);*/
            result = webMemberService.deltrue(id);
        }

        resultMap.put(IticConst.RESULT, result);
        return resultMap;
    }
    
    /**
     * 删除
     * 
     */
    @RequestMapping(value = "/categorys")
    @ResponseBody
    public List<WebmType> categorys() {
        WebmTypeExample example = new WebmTypeExample();
        Criteria criteria = example.createCriteria();
        criteria.andIsEffectiveEqualTo(DBConst.TRUE);
        return typeService.list(example);
    }
    
    
    
    

}